// PageCOM.cpp : implementation file
//

#include "stdafx.h"
#include "ECGWorkStation.h"
#include "PageCOM.h"
#include "..\public\pubfun\inireadwrite\inireadwrite.h"
#include "Globals.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPageCOM property page

IMPLEMENT_DYNCREATE(CPageCOM, CPropertyPage)

CPageCOM::CPageCOM() : CPropertyPage(CPageCOM::IDD)
{
	//{{AFX_DATA_INIT(CPageCOM)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CPageCOM::~CPageCOM()
{
}

void CPageCOM::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPageCOM)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPageCOM, CPropertyPage)
	//{{AFX_MSG_MAP(CPageCOM)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPageCOM message handlers

void CPageCOM::OnDestroy() 
{
	CPropertyPage::OnDestroy();
	
	nBPM = ((CComboBox*)GetDlgItem(IDC_COMBO_BMP))->GetCurSel();
	nDataBit = ((CComboBox*)GetDlgItem(IDC_COMBO_DATABIT))->GetCurSel();
	nStopBit = ((CComboBox*)GetDlgItem(IDC_COMBO_STOPBIT))->GetCurSel();
	nParityCheck = ((CComboBox*)GetDlgItem(IDC_COMBO_PARITYCHECK))->GetCurSel();
	nPortNo = ((CComboBox*)GetDlgItem(IDC_COMBO_PORTNO))->GetCurSel();

	CString strTemp = "";
	((CEdit*)GetDlgItem(IDC_EDIT_READ_TIME))->GetWindowText(strTemp);
	nReadTime = atoi(strTemp);
}

BOOL CPageCOM::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();

	LoadSetting();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPageCOM::LoadSetting()
{
	int nTemp = -1;
	for (int nLoop = 0; nLoop < sizeof(SETTING_PORTNO) / sizeof(const char*); ++nLoop)
	{
		((CComboBox*)GetDlgItem(IDC_COMBO_PORTNO))->AddString(SETTING_PORTNO[nLoop]);
	}
	CIniReadWrite::GetMe()->LoadInt(DEF_SETTING_COM, DEF_COM_PORTNO, &nTemp);
	((CComboBox*)GetDlgItem(IDC_COMBO_PORTNO))->SetCurSel(nTemp);

	for (nLoop = 0; nLoop < sizeof(SETTING_PARITYCHECK) / sizeof(const char*); ++nLoop)
	{
		((CComboBox*)GetDlgItem(IDC_COMBO_PARITYCHECK))->AddString((LPCTSTR)SETTING_PARITYCHECK[nLoop]);
	}
	CIniReadWrite::GetMe()->LoadInt(DEF_SETTING_COM, DEF_COM_PARITYCHECK, &nTemp);
	((CComboBox*)GetDlgItem(IDC_COMBO_PARITYCHECK))->SetCurSel(nTemp);

	for (nLoop = 0; nLoop < sizeof(SETTING_BPM) / sizeof(const char*); ++nLoop)
	{
		((CComboBox*)GetDlgItem(IDC_COMBO_BMP))->AddString(SETTING_BPM[nLoop]);
	}
	CIniReadWrite::GetMe()->LoadInt(DEF_SETTING_COM, DEF_COM_BPM, &nTemp);
	((CComboBox*)GetDlgItem(IDC_COMBO_BMP))->SetCurSel(nTemp);
	
	for (nLoop = 0; nLoop < sizeof(SETTING_DATABIT) / sizeof(const char*); ++nLoop)
	{
		((CComboBox*)GetDlgItem(IDC_COMBO_DATABIT))->AddString(SETTING_DATABIT[nLoop]);
	}
	CIniReadWrite::GetMe()->LoadInt(DEF_SETTING_COM, DEF_COM_DATABIT, &nTemp);
	((CComboBox*)GetDlgItem(IDC_COMBO_DATABIT))->SetCurSel(nTemp);

	for (nLoop = 0; nLoop < sizeof(SETTING_STOPBIT) / sizeof(const char*); ++nLoop)
	{
		((CComboBox*)GetDlgItem(IDC_COMBO_STOPBIT))->AddString(SETTING_STOPBIT[nLoop]);
	}
	CIniReadWrite::GetMe()->LoadInt(DEF_SETTING_COM, DEF_COM_STOPBIT, &nTemp);
	((CComboBox*)GetDlgItem(IDC_COMBO_STOPBIT))->SetCurSel(nTemp);

	CIniReadWrite::GetMe()->LoadInt(DEF_SETTING_COM, DEF_READ_TIME, &nReadTime);
	CString strTemp = "";
	strTemp.Format("%d", nReadTime);
	((CEdit*)GetDlgItem(IDC_EDIT_READ_TIME))->SetWindowText(strTemp);
}

void CPageCOM::SaveSetting()
{
	CIniReadWrite::GetMe()->SaveInt(DEF_SETTING_COM, DEF_COM_BPM, nBPM);
	CIniReadWrite::GetMe()->SaveInt(DEF_SETTING_COM, DEF_COM_PORTNO, nPortNo);
	CIniReadWrite::GetMe()->SaveInt(DEF_SETTING_COM, DEF_COM_DATABIT, nDataBit);
	CIniReadWrite::GetMe()->SaveInt(DEF_SETTING_COM, DEF_COM_STOPBIT, nStopBit);
	CIniReadWrite::GetMe()->SaveInt(DEF_SETTING_COM, DEF_COM_PARITYCHECK, nParityCheck);
	CIniReadWrite::GetMe()->SaveInt(DEF_SETTING_COM, DEF_READ_TIME, nReadTime);
}
